import java.util.TreeSet;

/**
 * @author LKQ
 * @date 2021/12/28 22:06
 * @description 采用TreeSet集合，或者用三个数a, b , c 记录最大值，次大值，第三大值
 */
public class Solution3 {
    public static void main(String[] args) {

    }
    public int thirdMax(int[] nums) {
        TreeSet<Integer> s = new TreeSet<Integer>();
        for (int num : nums) {
            s.add(num);
            if (s.size() > 3) {
                s.remove(s.first());
            }
        }
        return s.size() == 3 ? s.first() : s.last();
    }

    public int thirdMax2(int[] nums) {
        Integer a = null, b = null, c = null;
        for (int num : nums) {
            if (a == null || num > a) {
                c = b;
                b = a;
                a = num;
            } else if (a > num && (b == null || num > b)) {
                c = b;
                b = num;
            } else if (b != null && b > num && (c == null || num > c)) {
                c = num;
            }
        }
        return c == null ? a : c;
    }

}
